package com.gree.scmservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.enums.BusinessType;
import com.gree.common.utils.ReturnCode;
import com.gree.framework.entity.SysUser;
import com.gree.framework.service.GscmUserService;
import com.gree.scmservice.dao.SysUserDao;
import org.nutz.dao.entity.Record;
import org.nutz.mvc.annotation.PUT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author shqiwei
 */


@RestController
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    private GscmUserService gscmUserService;

    @Autowired
    private SysUserDao sysUserDao;

    @Log(title = "用户管理", businessType = BusinessType.SELECT)
    @GetMapping("/{usid}")
    public ReturnVo  updateUserInfo(@PathVariable String usid,@PathVariable String comp){
        return new ReturnVo(ReturnCode.SUCCESS,"success",gscmUserService.finduserWithClient(usid,comp,"greescmdb2"));
    }

    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ReturnVo  updateUserInfo(@RequestBody SysUser sysUser){
        return new ReturnVo(ReturnCode.SUCCESS,"success",gscmUserService.updateUser(sysUser));
    }

    @GetMapping("/getPassword")
    public String getPassword(@RequestParam("usid") String usid){
        return sysUserDao.getPassword(usid);
    }

    @Log(title = "根据usid获取部门", businessType = BusinessType.SELECT)
    @PostMapping("/getDept")
    public JSONObject selectByIdDept(@RequestParam(value = "usid",required = false) String usid, @RequestParam(value = "comp",required = false) String comp){
        JSONObject json = new JSONObject();
        try {
            Record record = gscmUserService.selectByIdDept(usid, comp);
            json.put("success",true);
            json.put("message","查询成功");
            json.put("data",record);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            json.put("success",false);
            json.put("message","查询失败");
            json.put("data",null);
            return json;
        }
    }

}
